//
//  UIButton+Extension.swift
//  微博
//
//  Created by 余仿 on 16/4/17.
//  Copyright © 2016年 YF. All rights reserved.
//

import UIKit

extension UIButton{

    convenience init(imageName:String?,title:String?,target:AnyObject?,action:Selector?,bgImage:String? = nil) {

        self.init()

        if let imgName = imageName{

            self.setImage(UIImage(named: imgName + "_highlighted"), forState: .Highlighted)

            self.setImage(UIImage(named: imgName), forState: .Normal)

            self.imageEdgeInsets = UIEdgeInsets(top: 0, left: -5, bottom: 0, right: 5)

        }

        if let bgImg = bgImage{

            self.setBackgroundImage(UIImage(named: bgImg), forState: .Normal)
            self.setBackgroundImage(UIImage(named: bgImg + "_highlighted"), forState: .Highlighted)
        }

        if let innerTitle = title{
            self.setTitle(innerTitle, forState: .Normal)
            self.titleLabel?.font = UIFont.systemFontOfSize(14)
            self.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
            self.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)

        }

        if let act = action ,let tar = target{
            self.addTarget(tar, action: act, forControlEvents: .TouchUpInside)
        }
        
        self.sizeToFit()
        
    }

}
